<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla view library
jimport('joomla.application.component.view');

/**
 * HTML View class for the HelloWorld Component
 */
class BeViewApplicationMatch extends JView {

	/**
	 * Default Index
	 */
	function display($tpl = null) {
		$application = &JFactory::getApplication();

		if($application->getUserStateFromRequest('application.match.id', 'id', 0, 'int') !== 0) {
			$tpl = 'element';
			$this->_renderDefaultElement();

		} else {
			$tpl = 'collection';
			$this->_renderDefaultCollection();
		}

		parent::display($tpl);
	}

	/**
	 * Uebersicht ueber die eingetragenen Spielstaende
	 * @param none
	 * @return void
	 */
	protected function _renderDefaultCollection() {
		$mApplicationMatch = JModel::getInstance('ApplicationMatch', 'BeModel');

		$this->assign('cApplicationMatch', $mApplicationMatch->findall());
	}

	/**
	 * Stellt einen Spielstand dar
	 * @param none
	 * @return void
	 */
	protected function _renderDefaultElement() {
		$application				= &JFactory::getApplication();

		// -----------------------------------------------------------------------------------
		// Spielfigur(en) auslesen und der Usersession zuweisen
		$user								= &JFactory::getUser();
		$mApplicationPlayer = JModel::getInstance('ApplicationPlayer', 'BeModel');
		$player = $mApplicationPlayer->find(array(
			'user'	=> $user->id,
			'match'	=> $application->getUserStateFromRequest('application.match.id', 'id', 0, 'int')
		));
		$application->setUserState('application.player.id', $player->id);
		

		// -----------------------------------------------------------------------------------
		// Planetenkarte erstellen
		$mApplicationPlanet = JModel::getInstance('ApplicationPlanet', 'BeModel');
		$this->assign('cApplicationPlanet', $mApplicationPlanet->findall(array(
			'match'	=> $application->getUserStateFromRequest('application.match.id', 'id', 0, 'int')
		)));
	}
}
?>